package org.jwcloud.project.module.zx.template.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.template.service.RptTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle",
description = "工作模板信息", tags = "work")
@RestController
@RequestMapping("/rest/rptWork")
public class RptWorkController extends BaseController{
	
//	@Autowired
//    private CommonService commonService;
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
    private RptTemplateService rptTemplateService;

    
    
    
    
    
    
    
    //------------
    @GetMapping("queryWorkList")
    @ApiOperation(value = "工作列表", notes = "主列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "template_id", value = "模板id", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_name", value = "工作名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "duty_dept", value = "责任部门id", required = false, dataType = "string", paramType = "query"),
    })
    public Resp<List<Map<String, String>>> queryWorkList(
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryWorkList(param);
    }
    
    @GetMapping("queryPriorWorkList")
    @ApiOperation(value = "可选的前置工作列表", notes = "主列表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "工作id", required = true, dataType = "int", paramType = "query"),
    	@ApiImplicitParam(name = "template_id", value = "模板id", required = true, dataType = "int", paramType = "query"),
    	
    })
    public Resp<List<Map<String, String>>> queryPriorWorkList(
    		@RequestParam(required = false) Map<String,String> param
    		){
        return rptTemplateService.queryPriorWorkList(param);
    }
    
    @PostMapping("doWorkInsert")
    @ApiOperation(value = "添加工作", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "template_id", value = "模板id", required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "work_name", value = "工作（不可重复）", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "app_phase", value = "项目阶段", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_type", value = "工作类型", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_idx", value = "工作序号", required = true, dataType = "int", paramType = "query"),
    	@ApiImplicitParam(name = "duty_dept", value = "工作序号", required = true, dataType = "int", paramType = "query"),
    	
    })
    public Resp<Object> doWorkInsert(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	String flag = FmtUtil.nullRes(param
    			, "template_id", "模板id"
    			, "work_name", "工作（不可重复）"
    			, "app_phase", "项目阶段"
    			, "work_type", "工作类型"
    			, "work_idx", "工作序号");
		if(!StringUtils.isEmpty(flag)) 
			Resp.error(flag);
        return rptTemplateService.doWorkInsert(param);
    }
    
    @PostMapping("doWorkUpdate")
    @ApiOperation(value = "修改工作", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "work_name", value = "工作名称", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "app_phase", value = "项目阶段", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_type", value = "工作类型", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "work_idx", value = "工作序号", required = true, dataType = "int", paramType = "query"),
    	
    })
    public Resp<Object> doWorkUpdate(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id"
				, "work_name", "工作名称"
				, "app_phase", "项目阶段"
				, "work_type", "工作类型"
				, "work_idx", "工作序号");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doWorkUpdate(param);
    }
    
    @DeleteMapping("doWorkDelete")
    @ApiOperation(value = "删除工作", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doWorkDelete(
    		@RequestParam(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doWorkDelete(param);
    }
    
    
    @DeleteMapping("doWorkDelete_bat")
    @ApiOperation(value = "批量删除工作", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id 1;2;3;4", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doWorkDelete_bat(
    		@RequestParam(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		String[] ids = param.get("id").split(";");
		Map map = new HashMap();
		for(String id: ids) {
			map.put("id", id);
			this.doWorkDelete(map);
		}
    	
        return Resp.success(1);
    }
    
    
    @PostMapping("doWorkInsertPrior")
    @ApiOperation(value = "添加前置工作", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "工作", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "priorIds", value = "前置id 1;2;3;4;5", required = true, dataType = "string", paramType = "query"),
    })
    public Resp<Object> doWorkInsertPrior(
    		@RequestBody(required = false) Map<String,String> param
    		){
    	
    	String flag = FmtUtil.nullRes(param
    			, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
    	
        return rptTemplateService.doWorkInsertPrior(param);
    }
    
	private List<Map<String, String>> quertCodeList(String tableName) {
		// TODO Auto-generated method stub
		return rptCommonRepository.queryCodeList(new HashMap() {{this.put("tableName", tableName);}});
	}
	
}